EvalExpression is a method which accepts a string and returns a double. The string passed to it can consist of numbers and the following mathematical operands:
+ adds one number to another
- subtracts one number from another
* multiplies one number to another
/ divides one number by another
% returns the remainder of one number by another
^ raises one number to the power of another
± returns the negative of a number
( and ) forces certain operations to be evaluated first.
_fcn(x) calls a function in the EvalFunction method (see below)
EvalExpression and the Order of Operations
EvalExpression uses the order of operations, as follows:
1. Operations in parenthesis are evaluated first
2. Negative numbers (±) are evaluated
3. Exponents are evaluated from left to right
4. Multiplication, Division, and Remainders are evaluated from left to right
5. Addition and Subtraction are evaluated from left to right.
Contacting the Author of EvalExpression
Hotline (at cafe.realbasic.com):
dzhim? [jim]
E-Mail:
dzhim@home.com
Internet:
http://members.home.net/dzhim/
EvalExpression Sample Code
Dim Expression as String, Result as Double
Expression = "-2"
Result = EvalExpression(Expression)
// Result = -2
Expression = "6+3"
Result = EvalExpression(Expression)
// Result = 9
Expression = "7-2*3"
Result = EvalExpression(Expression)
// Result = 1
Expression = "8+3^2"
Result = EvalExpression(Expression)
// Result = 17
Expression = "16%6"
Result = EvalExpression(Expression)
// Result = 4
Expression = "-5^2"
Result = EvalExpression(Expression)
// Result = -25
Expression = "-(5^2)"
Result = EvalExpression(Expression)
// Result = -25
Expression = "(-5)^2"
Result = EvalExpression(Expression)
// Result = 25
Expression = "±5^2"
Result = EvalExpression(Expression)
// Result = 25
Expression = "2/5+(2-1)"
Result = EvalExpression(Expression)
// Result = 1.4
Expression = "6(7-2)-1"
Result = EvalExpression(Expression)
// Result = 29
Expression = "5+3*(4-1)/2^2"
Result = EvalExpression(Expression)
// Result = 7.25
Expression = "_pi()"
Result = EvalExpression(Expression)
// Result = 3.1415927
Expression = "_abs(_floor(-5.3)+1)"
Result = EvalExpression(Expression)
// Result = 5
EvalExpression Functions
EvalExpression 1.3 supports functions.
_sin(x) returns the sine of x
_cos(x) returns the cosine of x
_tan(x) returns the tangent of x
_asin(x) returns the arc sine of x
_acos(x) returns the arc cosine of x
_atan(x) returns the arc tangent of x
_log(x) returns the base ten logarithm of x
_ln(x) returns the base e logarithm of x
_abs(x) returns the absolute value of x
_rand(x) returns a random number between 0 and x
_round(x) rounds x to the nearest whole number
_floor(x) rounds x down to a whole number
_ceil(x) rounds x up to a whole number
_pi() returns a decimal approximation of pi
EvalExpression Version History
1.3
Added support for mathematical functions such as _sin or _abs
1.2
Added % operand for remainders
Added ± operand for the opposite of a number
Fixed bugs with (-a)*b, (-a)^b, etc., operations
1.1
Fixed bugs with multiple, non-nested parenthesis
Added support for a(b), (a)(b), and (a)b multiplication
1.0
Initial release
EvalExpression Legal Info
EvalExpression is free to use and distribute. You may use it in any application that you make, provided that I am given fair credit for EvalExpression. If you use it in a shareware or commercial product, contact me and we can work out a deal.
EvalExpression is open source: that is, you are free to copy or modify any parts of the source code for EvalExpression, provided that you send a copy of the modified source file to me so that we can learn from each other's progress.